package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.WcsReport.WcsReportService;
import me.zhengjie.agvSend.AgvSendService;
import me.zhengjie.pojo.dto.*;
import me.zhengjie.pojo.entity.*;
import me.zhengjie.pojo.vo.AgvTaskSubmitVo;
import me.zhengjie.pojo.vo.ReturnVo;
import me.zhengjie.service.DeliveryService;
import me.zhengjie.service.SysLogService;
import me.zhengjie.sqlservice.*;
import me.zhengjie.utility.IdWorker;
import me.zhengjie.utility.liftState.PriorityEnum;
import me.zhengjie.utility.state.TaskStatus;
import me.zhengjie.utility.state.TaskType;
import net.dreamlu.mica.core.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class DeliveryServiceImpl implements DeliveryService {

    private static final Logger outbound = LoggerFactory.getLogger("wcsOutbound");

    private final AgvSendService agvSendService;

    private final IdWorker idWorker;

    private final WcsOutboundMissionService wcsOutboundMissionService;

    private final WcsInboundMissionService wcsInboundMissionService;

    private final WcsDeliveryMissionService wcsDeliveryMissionService;

    private final WcsLineEdgeWarehouseService wcsLineEdgeWarehouseService;

    private final WcsThreeFloorOutboundService wcsThreeFloorOutboundService;

    private final WcsThreeFloorOutboundDetailsService wcsThreeFloorOutboundDetailsService;

    private final NestingMiddleTableService nestingMiddleTableService;

    private final WcsReportService wcsReportService;

    private final WcsAgvReleaseGoodsService wcsAgvReleaseGoodsService;

    private final SysLogService sysLogService;

    @Override
    @Transactional
    public ReturnVo deliveryTask(DeliveryDto oneFloorOutboundDto) {
        Integer deliveryType = oneFloorOutboundDto.getDeliveryType();
        String floor = oneFloorOutboundDto.getFloor();
        String source = oneFloorOutboundDto.getSource();
        //终点大致区域
        String destination = oneFloorOutboundDto.getDestination();
        Integer priority = oneFloorOutboundDto.getPriority();
        String taskType = oneFloorOutboundDto.getTaskType();
        String materIalCode = oneFloorOutboundDto.getMaterIalCode();
        String wmsTaskId = oneFloorOutboundDto.getWmsTaskId();
        String containerCode = oneFloorOutboundDto.getContainerCode();
        if (containerCode == null || source == null ) {
            return ReturnVo.ng("WMS传参为null");
        }
        if (deliveryType == 7) {
            //配送产线任务
            List<WcsLineEdgeWarehouse> wcsLineEdgeWarehouses = wcsLineEdgeWarehouseService.selectRegionStatusIsNull(floor,destination, TaskStatus.LINE_EDGE_WAREHOUSE_IDLE.getStatus());
            if (!wcsLineEdgeWarehouses.isEmpty()) {
                List<WcsDeliveryMission> wcsDeliveryMissionList = wcsDeliveryMissionService.selectContainerCodeNoTask(containerCode);
                if (!wcsDeliveryMissionList.isEmpty()){
                    outbound.error("载具已再AGV任务中:{}",containerCode);
                    return new ReturnVo(500,"载具已再AGV任务中",null);
                }
                String deliveryId = String.valueOf(idWorker.nextId());
                WcsLineEdgeWarehouse wcsLineEdgeWarehouse = wcsLineEdgeWarehouses.get(0);
                String storageCode = wcsLineEdgeWarehouse.getStorageCode();
                WcsDeliveryMission wcsDeliveryMission = new WcsDeliveryMission();
                wcsDeliveryMission.setDeliveryCode(deliveryId);
                wcsDeliveryMission.setMissionStatus(TaskStatus.task_send.getStatus());
                wcsDeliveryMission.setContainerCode(containerCode);
                wcsDeliveryMission.setSource(source);
                wcsDeliveryMission.setDestination(storageCode);
                wcsDeliveryMission.setCreateTime(LocalDateTime.now());
                wcsDeliveryMission.setUpdateTime(LocalDateTime.now());
                wcsDeliveryMission.setContainerType(TaskType.DELIVERY.getStatus());
                wcsDeliveryMission.setReportWms(1);
                wcsDeliveryMission.setFloor(floor);
                boolean b1 = wcsDeliveryMissionService.insertDelivery(wcsDeliveryMission);
                if (!b1) {
                   outbound.error("数据库异常,写入数据库失败");
                   throw new RuntimeException("数据库异常,写入数据库失败");
                }
                //产线上有空储位
                //储位绑定栈板
                CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
                carrierUnbindDto.setSlotCategory(TaskType.SITE.getMessage());
                carrierUnbindDto.setSlotCode(source);
                carrierUnbindDto.setCarrierCategory("PALLET");
                carrierUnbindDto.setCarrierType("1");
                carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
                agvSendService.carrierUnbind(carrierUnbindDto);

                AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                //todo 任务类型，根据现场情况来定
                agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
                ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                TargetRoute targetRoute = new TargetRoute();
                TargetRoute targetRoutes = new TargetRoute();
                //添加起点
                targetRoute.setSeq(0);
                targetRoute.setType(TaskType.STORAGE.getMessage());
                targetRoute.setCode(source);
                //添加终点
                targetRoutes.setSeq(1);
                targetRoutes.setType(TaskType.SITE.getMessage());
                targetRoutes.setCode(storageCode);
                targetRouteList.add(targetRoute);
                targetRouteList.add(targetRoutes);
                agvTaskSubmitDto.setTargetRoute(targetRouteList);
                agvTaskSubmitDto.setRobotTaskCode(deliveryId);
                agvTaskSubmitDto.setInitPriority(priority);
                String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                if (taskCode != null) {
                    //将线边仓储位预定
                    boolean b = wcsLineEdgeWarehouseService.updateStatusIsAgv(storageCode,null, TaskStatus.LINE_EDGE_WAREHOUSE_RESERVED.getStatus(), 1);
                    if (!b) {
                        outbound.error("数据库异常，修改线边仓储位失败");
                        throw new RuntimeException("数据库异常，修改线边仓储位失败");
                    }
                    return ReturnVo.OK();
                }
            } else {
                //配送任务，wms给了区域，没找到空储位，将任务记录，定时找空储位
                String deliveryId = String.valueOf(idWorker.nextId());
                WcsDeliveryMission wcsDeliveryMission = new WcsDeliveryMission();
                wcsDeliveryMission.setDeliveryCode(deliveryId);
                wcsDeliveryMission.setMissionStatus(TaskStatus.Task_create.getStatus());
                wcsDeliveryMission.setContainerCode(containerCode);
                wcsDeliveryMission.setSource(source);
                wcsDeliveryMission.setDestination(destination);
                wcsDeliveryMission.setCreateTime(LocalDateTime.now());
                wcsDeliveryMission.setUpdateTime(LocalDateTime.now());
                wcsDeliveryMission.setContainerType(TaskType.DELIVERY.getStatus());
                wcsDeliveryMission.setReportWms(1);
                wcsDeliveryMission.setFloor(floor);
                boolean b1 = wcsDeliveryMissionService.insertDelivery(wcsDeliveryMission);
                if (!b1) {
                    outbound.error("点对点叫车，创建配送任务失败，数据库异常");
                    throw new RuntimeException("点对点叫车，创建配送任务失败，数据库异常");
                }
            }
            return ReturnVo.ng("配送到线边仓无空储位");
        } else {
            if ("M1".equals(floor)) {
                if (deliveryType == 2) {
                    List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectContainerCodeNotTask(containerCode);
                    if (!wcsOutboundMissionList.isEmpty()){
                        return new ReturnVo(500,"载具已再AGV任务中",null);
                    }
                    String outboundCode = String.valueOf(idWorker.nextId());
                    //创建出库任务
                    WcsOutboundMission wcsOutboundMission = new WcsOutboundMission();
                    wcsOutboundMission.setOutboundCode(outboundCode);
                    wcsOutboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                    wcsOutboundMission.setContainerCode(containerCode);
                    wcsOutboundMission.setSource(source);
                    wcsOutboundMission.setDestination(destination);
                    wcsOutboundMission.setCreateTime(LocalDateTime.now());
                    wcsOutboundMission.setUpdateTime(LocalDateTime.now());
                    wcsOutboundMission.setPriority(priority);
                    wcsOutboundMission.setTaskType(taskType);
                    wcsOutboundMission.setContainerType(TaskType.PALLET.getStatus());
                    wcsOutboundMission.setIsNest(0);
                    wcsOutboundMission.setWmsTaskId(wmsTaskId);
                    wcsOutboundMission.setFloor(floor);
                    wcsOutboundMission.setIsWms(1);
                    boolean b = wcsOutboundMissionService.insertOutbound(wcsOutboundMission);
                    if (!b) {
                        outbound.error("数据库异常，添加出库任务失败");
                        throw new RuntimeException("数据库异常，添加出库任务失败");
                    }
                    //储位绑定栈板
                    CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
                    carrierUnbindDto.setSlotCategory(TaskType.SITE.getMessage());
                    carrierUnbindDto.setSlotCode(source);
                    carrierUnbindDto.setCarrierCategory("PALLET");
                    carrierUnbindDto.setCarrierType("1");
                    carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
//                    agvSendService.carrierUnbind(carrierUnbindDto);
                    AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                    //todo 任务类型，根据现场情况来定
                    agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
                    ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                    TargetRoute targetRoute = new TargetRoute();
                    TargetRoute targetRoutes = new TargetRoute();
                    //添加起点
                    targetRoute.setSeq(0);
                    targetRoute.setType(TaskType.STORAGE.getMessage());
                    targetRoute.setCode(source);
                    //添加终点
                    targetRoutes.setSeq(1);
                    targetRoutes.setType(TaskType.SITE.getMessage());
                    targetRoutes.setCode(destination);
                    targetRouteList.add(targetRoute);
                    targetRouteList.add(targetRoutes);
                    agvTaskSubmitDto.setTargetRoute(targetRouteList);
                    agvTaskSubmitDto.setRobotTaskCode(outboundCode);
                    agvTaskSubmitDto.setInitPriority(priority);
//                    String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
//                    if (taskCode != null) {
//                        return ReturnVo.OK();
//                    }
//                    return ReturnVo.ng("任务下发给AGVC失败");
                    return ReturnVo.OK();
                } else if (deliveryType == 4) {
                    String inboundCode = String.valueOf(idWorker.nextId());
                    List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectContainerCodeNotTask(containerCode);
                    if (!wcsInboundMissionList.isEmpty()){
                        return new ReturnVo(500,"载具已再AGV任务中",null);
                    }
                    //创建入库任务
                    WcsInboundMission wcsInboundMission = new WcsInboundMission();
                    wcsInboundMission.setInboundCode(inboundCode);
                    wcsInboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                    wcsInboundMission.setContainerCode(containerCode);
                    wcsInboundMission.setSource(source);
                    wcsInboundMission.setDestination(destination);
                    wcsInboundMission.setCreateTime(LocalDateTime.now());
                    wcsInboundMission.setUpdateTime(LocalDateTime.now());
                    wcsInboundMission.setPriority(priority);
                    wcsInboundMission.setInboundType(TaskType.circle_library_sorting.getStatus());
                    wcsInboundMission.setTaskType(taskType);
                    wcsInboundMission.setContainerType(TaskType.PALLET.getStatus());
                    wcsInboundMission.setIsNest(1);
                    wcsInboundMission.setIsPlc(0);
                    wcsInboundMission.setMaterialCode(materIalCode);
                    wcsInboundMission.setWmsTaskId(wmsTaskId);
                    wcsInboundMission.setFloor(floor);
                    wcsInboundMission.setIsWms(1);
                    boolean b = wcsInboundMissionService.insertInbound(wcsInboundMission);
                    if (!b) {
                        outbound.error("数据库异常，添加出库任务失败");
                        throw new RuntimeException("数据库异常，添加出库任务失败");
                    }
                    //分拣回库，下发到测高plc
                    //储位绑定栈板
                    CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
                    carrierUnbindDto.setSlotCategory(TaskType.SITE.getMessage());
                    carrierUnbindDto.setSlotCode(source);
                    carrierUnbindDto.setCarrierCategory("PALLET");
                    carrierUnbindDto.setCarrierType("1");
                    carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
                    agvSendService.carrierUnbind(carrierUnbindDto);
                    AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                    //todo 任务类型，根据现场情况来定
                    agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_TASK_CONTINUE.getMessage());
                    ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                    TargetRoute targetRoute = new TargetRoute();
                    TargetRoute targetRoutes = new TargetRoute();
                    //添加起点
                    targetRoute.setSeq(0);
                    targetRoute.setType(TaskType.STORAGE.getMessage());
                    targetRoute.setCode(source);
                    //添加终点
                    targetRoutes.setSeq(1);
                    targetRoutes.setType(TaskType.SITE.getMessage());
                    targetRoutes.setCode(destination);
                    targetRouteList.add(targetRoute);
                    targetRouteList.add(targetRoutes);
                    agvTaskSubmitDto.setTargetRoute(targetRouteList);
                    agvTaskSubmitDto.setRobotTaskCode(inboundCode);
                    agvTaskSubmitDto.setInitPriority(priority);
                    String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                    if (taskCode != null) {
                        return ReturnVo.OK();
                    }
                    outbound.error("任务下发给AGVC失败");
                    throw new RuntimeException("任务下发给AGVC失败");
                }
            } else if ("M2".equals(floor)) {
                if (deliveryType == 2) {
                    List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectContainerCodeNotTask(containerCode);
                    if (!wcsOutboundMissionList.isEmpty()){
                        outbound.error("载具已再AGV任务中:{}",containerCode);
                        return new ReturnVo(500,"载具已再AGV任务中",null);
                    }
                    //2楼出库，添加到出库任务，需要通过plc才知道能不能放货
                    String outboundCode = String.valueOf(idWorker.nextId());
                    //创建出库任务
                    WcsOutboundMission wcsOutboundMission = new WcsOutboundMission();
                    wcsOutboundMission.setOutboundCode(outboundCode);
                    wcsOutboundMission.setMissionStatus(TaskStatus.Task_create.getStatus());
                    wcsOutboundMission.setContainerCode(oneFloorOutboundDto.getContainerCode());
                    wcsOutboundMission.setSource(source);
                    wcsOutboundMission.setDestination(destination);
                    wcsOutboundMission.setCreateTime(LocalDateTime.now());
                    wcsOutboundMission.setUpdateTime(LocalDateTime.now());
                    wcsOutboundMission.setContainerType(TaskType.WORKBIN.getStatus());
                    wcsOutboundMission.setPriority(priority);
                    wcsOutboundMission.setTaskType(taskType);
                    wcsOutboundMission.setIsNest(0);
                    wcsOutboundMission.setWmsTaskId(wmsTaskId);
                    wcsOutboundMission.setFloor(floor);
                    wcsOutboundMission.setIsWms(1);
                    boolean b = wcsOutboundMissionService.insertOutbound(wcsOutboundMission);
                    if (!b) {
                        outbound.error("数据库异常，添加出库任务失败");
                        throw new RuntimeException("数据库异常，添加出库任务失败");
                    }
                    return ReturnVo.OK();
                }else if (deliveryType == 9){
                    List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectContainerCodeNotTask(containerCode);
                    if (!wcsOutboundMissionList.isEmpty()){
                        outbound.error("载具已再AGV任务中:{}",containerCode);
                        return new ReturnVo(500,"载具已再AGV任务中",null);
                    }
                    //2楼出库到抽检区
                    String outboundCode = String.valueOf(idWorker.nextId());
                    AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
//
                    agvTaskSubmitDto.setTaskType("CTU-KUNEI");
                    ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                    TargetRoute targetRoute = new TargetRoute();
                    TargetRoute targetRoutes = new TargetRoute();
                    //添加起点
                    targetRoute.setSeq(0);
                    targetRoute.setType(TaskType.STORAGE.getMessage());
                    targetRoute.setCode(source);
                    //添加终点
                    targetRoutes.setSeq(1);
                    targetRoutes.setType(TaskType.STORAGE.getMessage());
                    targetRoutes.setCode(destination);
                    targetRouteList.add(targetRoute);
                    targetRouteList.add(targetRoutes);
                    agvTaskSubmitDto.setTargetRoute(targetRouteList);
                    agvTaskSubmitDto.setRobotTaskCode(outboundCode);
                    agvTaskSubmitDto.setInitPriority(priority);

                    CarrierInfoDto carrierInfoDto = new CarrierInfoDto();
                    carrierInfoDto.setCarrierCode(containerCode);
                    carrierInfoDto.setCarrierType(TaskType.TWO_Task_assignment_Container_binding.getMessage());
                    ArrayList<CarrierInfoDto> carrierInfoDtos = new ArrayList<>();
                    carrierInfoDtos.add(carrierInfoDto);
                    ExtraDto extraDto = new ExtraDto();
                    extraDto.setCarrierInfo(carrierInfoDtos);
                    agvTaskSubmitDto.setExtra(extraDto);
                    String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                    if (taskCode != null) {
                        //创建出库任务
                        WcsOutboundMission wcsOutboundMission = new WcsOutboundMission();
                        wcsOutboundMission.setOutboundCode(outboundCode);
                        wcsOutboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                        wcsOutboundMission.setContainerCode(containerCode);
                        wcsOutboundMission.setSource(source);
                        wcsOutboundMission.setDestination(destination);
                        wcsOutboundMission.setCreateTime(LocalDateTime.now());
                        wcsOutboundMission.setUpdateTime(LocalDateTime.now());
                        wcsOutboundMission.setPriority(priority);
                        wcsOutboundMission.setTaskType(taskType);
                        wcsOutboundMission.setContainerType(TaskType.WORKBIN.getStatus());
                        wcsOutboundMission.setIsNest(1);
                        wcsOutboundMission.setWmsTaskId(wmsTaskId);
                        wcsOutboundMission.setFloor(floor);
                        wcsOutboundMission.setIsWms(1);
                        boolean b = wcsOutboundMissionService.insertOutbound(wcsOutboundMission);
                        if (!b) {
                            outbound.error("数据库异常，添加出库任务失败");
                            throw new RuntimeException("数据库异常，添加出库任务失败");
                        }
                        return ReturnVo.OK();
                    }
                    return ReturnVo.ng("任务下发给AGVC失败");
                }
            } else if ("M3".equals(floor)) {
                if (deliveryType == 2) {
                    String outboundCode = String.valueOf(idWorker.nextId());
                    //出库，添加到出库任务
                    List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectContainerCodeNotTask(containerCode);
                    if (!wcsOutboundMissionList.isEmpty()){
                        return new ReturnVo(500,"载具已再AGV任务中",null);
                    }
                    //储位绑定栈板
//                    CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
//                    carrierUnbindDto.setSlotCategory(TaskType.STORAGE.getMessage());
//                    carrierUnbindDto.setSlotCode(source);
//                    carrierUnbindDto.setCarrierCategory("PALLET");
//                    carrierUnbindDto.setCarrierType("1");
//                    carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
//                    agvSendService.carrierUnbind(carrierUnbindDto);
                    AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                    //todo 任务类型，根据现场情况来定
//                    agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_TASK_CONTINUE.getMessage());
                    agvTaskSubmitDto.setTaskType("CTU-3F-YK");
                    ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                    TargetRoute targetRoute = new TargetRoute();
                    TargetRoute targetRoutes = new TargetRoute();
                    //添加起点
                    targetRoute.setSeq(0);
                    targetRoute.setType(TaskType.STORAGE.getMessage());
                    targetRoute.setCode(source);
                    //添加终点
                    targetRoutes.setSeq(1);
//                    targetRoutes.setType(TaskType.SITE.getMessage());
                    targetRoutes.setType(TaskType.STORAGE.getMessage());
                    targetRoutes.setCode(destination);
                    targetRouteList.add(targetRoute);
                    targetRouteList.add(targetRoutes);
                    agvTaskSubmitDto.setTargetRoute(targetRouteList);
                    agvTaskSubmitDto.setRobotTaskCode(outboundCode);
                    agvTaskSubmitDto.setInitPriority(priority);

                    CarrierInfoDto carrierInfoDto = new CarrierInfoDto();
                    carrierInfoDto.setCarrierCode(containerCode);
                    carrierInfoDto.setCarrierType(TaskType.THREE_Task_assignment_Container_binding.getMessage());
                    ArrayList<CarrierInfoDto> carrierInfoDtos = new ArrayList<>();
                    carrierInfoDtos.add(carrierInfoDto);
                    ExtraDto extraDto = new ExtraDto();
                    extraDto.setCarrierInfo(carrierInfoDtos);
                    agvTaskSubmitDto.setExtra(extraDto);
                    String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                    if (taskCode != null) {
                        //创建出库任务
                        WcsOutboundMission wcsOutboundMission = new WcsOutboundMission();
                        wcsOutboundMission.setOutboundCode(outboundCode);
                        wcsOutboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                        wcsOutboundMission.setContainerCode(containerCode);
                        wcsOutboundMission.setSource(source);
                        wcsOutboundMission.setDestination(destination);
                        wcsOutboundMission.setCreateTime(LocalDateTime.now());
                        wcsOutboundMission.setUpdateTime(LocalDateTime.now());
                        wcsOutboundMission.setContainerType(TaskType.THREE_WORKBIN.getStatus());
                        wcsOutboundMission.setPriority(priority);
                        wcsOutboundMission.setTaskType(taskType);
                        wcsOutboundMission.setIsNest(0);
                        wcsOutboundMission.setWmsTaskId(wmsTaskId);
                        wcsOutboundMission.setFloor(floor);
                        wcsOutboundMission.setIsWms(1);
                        boolean b = wcsOutboundMissionService.insertOutbound(wcsOutboundMission);
                        if (!b) {
                            outbound.error("数据库异常，添加出库任务失败");
                            throw new RuntimeException("数据库异常，添加出库任务失败");
                        }
                        return ReturnVo.OK();
                    }
                    outbound.error("下发agv任务失败");
                   throw new RuntimeException("下发agv任务失败");
                }else if (deliveryType == 9){
                    List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectContainerCodeNotTask(containerCode);
                    if (!wcsOutboundMissionList.isEmpty()){
                        outbound.error("载具已再AGV任务中:{}",containerCode);
                        return new ReturnVo(500,"载具已再AGV任务中",null);
                    }
                    //2楼出库到抽检区
                    //创建出库任务
                    String outboundCode = String.valueOf(idWorker.nextId());
                    AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
//                    //todo 任务类型，根据现场情况来定
                    agvTaskSubmitDto.setTaskType("CTU-3F-YK");
                    ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                    TargetRoute targetRoute = new TargetRoute();
                    TargetRoute targetRoutes = new TargetRoute();
                    //添加起点
                    targetRoute.setSeq(0);
                    targetRoute.setType(TaskType.STORAGE.getMessage());
                    targetRoute.setCode(source);
                    //添加终点
                    targetRoutes.setSeq(1);
                    targetRoutes.setType(TaskType.STORAGE.getMessage());
                    targetRoutes.setCode(destination);
                    targetRouteList.add(targetRoute);
                    targetRouteList.add(targetRoutes);
                    agvTaskSubmitDto.setTargetRoute(targetRouteList);
                    agvTaskSubmitDto.setRobotTaskCode(outboundCode);
                    agvTaskSubmitDto.setInitPriority(priority);

                    CarrierInfoDto carrierInfoDto = new CarrierInfoDto();
                    carrierInfoDto.setCarrierCode(containerCode);
                    carrierInfoDto.setCarrierType(TaskType.THREE_Task_assignment_Container_binding.getMessage());
                    ArrayList<CarrierInfoDto> carrierInfoDtos = new ArrayList<>();
                    carrierInfoDtos.add(carrierInfoDto);
                    ExtraDto extraDto = new ExtraDto();
                    extraDto.setCarrierInfo(carrierInfoDtos);
                    agvTaskSubmitDto.setExtra(extraDto);
                    String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                    if (taskCode != null) {
                        WcsOutboundMission wcsOutboundMission = new WcsOutboundMission();
                        wcsOutboundMission.setOutboundCode(outboundCode);
                        wcsOutboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                        wcsOutboundMission.setContainerCode(containerCode);
                        wcsOutboundMission.setSource(source);
                        wcsOutboundMission.setDestination(destination);
                        wcsOutboundMission.setCreateTime(LocalDateTime.now());
                        wcsOutboundMission.setUpdateTime(LocalDateTime.now());
                        wcsOutboundMission.setPriority(priority);
                        wcsOutboundMission.setTaskType(taskType);
                        wcsOutboundMission.setContainerType(TaskType.THREE_WORKBIN.getStatus());
                        wcsOutboundMission.setIsNest(1);
                        wcsOutboundMission.setWmsTaskId(wmsTaskId);
                        wcsOutboundMission.setFloor(floor);
                        wcsOutboundMission.setIsWms(1);
                        boolean b = wcsOutboundMissionService.insertOutbound(wcsOutboundMission);
                        if (!b) {
                            outbound.error("数据库异常，添加出库任务失败");
                            throw new RuntimeException("数据库异常，添加出库任务失败");
                        }
                        return ReturnVo.OK();
                    }
                    return ReturnVo.ng("任务下发给AGVC失败");
                }
            }
            if (deliveryType == 1 || deliveryType == 3 || deliveryType == 5 || deliveryType == 6) {
                String inboundCode = String.valueOf(idWorker.nextId());
                List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectContainerCodeNotTask(containerCode);
                if (!wcsInboundMissionList.isEmpty()){
                    return new ReturnVo(500,"载具已再AGV任务中",null);
                }
                //创建入库任务
                WcsInboundMission wcsInboundMission = new WcsInboundMission();
                wcsInboundMission.setInboundCode(inboundCode);
                wcsInboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                wcsInboundMission.setContainerCode(containerCode);
                wcsInboundMission.setSource(source);
                wcsInboundMission.setDestination(destination);
                wcsInboundMission.setCreateTime(LocalDateTime.now());
                wcsInboundMission.setUpdateTime(LocalDateTime.now());
                wcsInboundMission.setPriority(priority);
                switch (deliveryType) {
                    case 1:
                        wcsInboundMission.setInboundType(TaskType.inbound_ordinary.getStatus());
                        break;
                    case 3:
                        wcsInboundMission.setInboundType(TaskType.circle_library_stocktaking.getStatus());
                        break;
                    case 4:
                        wcsInboundMission.setInboundType(TaskType.circle_library_sorting.getStatus());
                        break;
                    case 5:
                        wcsInboundMission.setInboundType(TaskType.circle_library_mould_assembling.getStatus());
                        break;
                    case 6:
                        wcsInboundMission.setInboundType(TaskType.FREEZE_AND_RETURN_TO_STORAGE.getStatus());
                        break;
                }
                wcsInboundMission.setTaskType(taskType);
                if (Objects.equals(floor, "M1")) {
                    wcsInboundMission.setContainerType(TaskType.PALLET.getStatus());
                } else if (Objects.equals(floor, "M2")) {
                    wcsInboundMission.setContainerType(TaskType.WORKBIN.getStatus());
                } else if (Objects.equals(floor, "M3")) {
                    wcsInboundMission.setContainerType(TaskType.THREE_WORKBIN.getStatus());
                }
                if (deliveryType != 1) {
                    wcsInboundMission.setIsNest(1);
                } else {
                    wcsInboundMission.setIsNest(0);
                }
                wcsInboundMission.setIsPlc(0);
                wcsInboundMission.setMaterialCode(materIalCode);
                wcsInboundMission.setWmsTaskId(wmsTaskId);
                wcsInboundMission.setFloor(floor);
                wcsInboundMission.setIsWms(1);
                boolean b = wcsInboundMissionService.insertInbound(wcsInboundMission);
                if (!b) {
                    outbound.error("数据库异常，添加出库任务失败");
                    throw new RuntimeException("数据库异常，添加出库任务失败");
                }
                //储位绑定栈板
//                CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
//                carrierUnbindDto.setSlotCategory(TaskType.SITE.getMessage());
//                carrierUnbindDto.setSlotCode(source);
//                carrierUnbindDto.setCarrierCategory("PALLET");
//                carrierUnbindDto.setCarrierType("1");
//                carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
//                agvSendService.carrierUnbind(carrierUnbindDto);

                AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                agvTaskSubmitDto.setTaskType("PF-CTU-RUKU");
                ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                TargetRoute targetRoute = new TargetRoute();
                TargetRoute targetRoutes = new TargetRoute();
                //添加起点
                targetRoute.setSeq(0);
                targetRoute.setType(TaskType.STORAGE.getMessage());
                targetRoute.setCode(source);
                //添加终点
                targetRoutes.setSeq(1);
                targetRoutes.setType(TaskType.STORAGE.getMessage());
                targetRoutes.setCode(destination);
                targetRouteList.add(targetRoute);
                targetRouteList.add(targetRoutes);
                agvTaskSubmitDto.setTargetRoute(targetRouteList);
                agvTaskSubmitDto.setRobotTaskCode(inboundCode);
                agvTaskSubmitDto.setInitPriority(priority);

                CarrierInfoDto carrierInfoDto = new CarrierInfoDto();
                carrierInfoDto.setCarrierCode(containerCode);
                carrierInfoDto.setCarrierType(TaskType.THREE_Task_assignment_Container_binding.getMessage());
                ArrayList<CarrierInfoDto> carrierInfoDtos = new ArrayList<>();
                carrierInfoDtos.add(carrierInfoDto);
                ExtraDto extraDto = new ExtraDto();
                extraDto.setCarrierInfo(carrierInfoDtos);
                agvTaskSubmitDto.setExtra(extraDto);
                String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                if (taskCode != null) {
                    return ReturnVo.OK();
                }else {
                    outbound.error("任务下发失败");
                    throw new RuntimeException("任务下发失败");
                }
            }
        }
        return ReturnVo.ng("任务下发给AGVC失败");
    }

    @Override
    public ReturnVo inboundTask() {
        List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectLtStatus(TaskStatus.Task_complete.getStatus());
        List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectLtStatus(TaskStatus.Task_complete.getStatus());
        if (wcsInboundMissionList.isEmpty() && wcsOutboundMissionList.isEmpty()) {
            return ReturnVo.OK(false);
        }
        return ReturnVo.OK(true);
    }

    @Override
    @Transactional
    public ReturnVo threeFloorOutbound(ThreeFloorOutboundDto threeFloorOutboundDto) {
        String workOrder = threeFloorOutboundDto.getWorkOrder();
        Integer total = threeFloorOutboundDto.getTotal();
        String region = threeFloorOutboundDto.getRegion();
        List<DeliveryDto> deliveryDtoLift1 = threeFloorOutboundDto.getDeliveryDtoLift();
        int size = threeFloorOutboundDto.getDeliveryDtoLift().size();
        for (DeliveryDto deliveryDto : deliveryDtoLift1) {
            if (deliveryDto.getSource() == null || deliveryDto.getDestination() ==null){
                outbound.info("3F下发出库，返回错误,起点或终点传参为null");
                return ReturnVo.ng("起点或终点传参为null");
            }
        }
        if (deliveryDtoLift1.isEmpty() || total == 0){
            outbound.info("任务下发参数为0");
            return ReturnVo.ng("任务下发参数为0");
        }
        if (workOrder == null || total == null || total != size) {
            outbound.info("3F下发出库，返回错误,传参为null或下发任务数量不一致");
            return ReturnVo.ng("传参为null或下发任务数量不一致");
        }
//        Integer wmsCount = wcsThreeFloorOutboundService.selectWmsStatusCount(0);
//        if (wmsCount >= 2){
//            outbound.info("3F下发出库，返回错误,3F下发出库单大于2这张");
//            return ReturnVo.ng("3F下发出库单大于2这张");
//        }
//        List<WcsThreeFloorOutbound> wcsThreeFloorOutbounds = wcsThreeFloorOutboundService.selectNoMechanicalArmCompleteList(0);
//        if (wcsThreeFloorOutbounds.size() >=2){
//            return ReturnVo.ng("出库单过多");
//        }
        //记录当前出库单
        WcsThreeFloorOutbound wcsThreeFloorOutbound = new WcsThreeFloorOutbound();
        wcsThreeFloorOutbound.setWorkOrder(workOrder);
        wcsThreeFloorOutbound.setTotal(total);
        wcsThreeFloorOutbound.setCompleteNumber(0);
        wcsThreeFloorOutbound.setMechanicalArmComplete(0);
        wcsThreeFloorOutbound.setCreateTime(LocalDateTime.now());
        wcsThreeFloorOutbound.setWmsStatus(0);
        boolean b = wcsThreeFloorOutboundService.insert(wcsThreeFloorOutbound);
        if (!b) {
            outbound.error("3楼出库单记录失败");
            throw new RuntimeException("3楼出库单记录失败");
        }
        List<DeliveryDto> deliveryDtoLift = threeFloorOutboundDto.getDeliveryDtoLift();
        for (DeliveryDto deliveryDto : deliveryDtoLift) {
            String source = deliveryDto.getSource();
            String destination = deliveryDto.getDestination();
            Integer priority = deliveryDto.getPriority();
            String containerCode = deliveryDto.getContainerCode();
            String taskType = deliveryDto.getTaskType();
            String wmsTaskId = deliveryDto.getWmsTaskId();
            String floor = deliveryDto.getFloor();
            //3楼plc,下发中间点模版任务
            //出库，添加到出库任务
            String outboundCode = String.valueOf(idWorker.nextId());
            List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectContainerCodeNotTask(containerCode);
            if (!wcsOutboundMissionList.isEmpty()){
                return new ReturnVo(500,"载具已再AGV任务中",null);
            }
            //创建出库任务
            WcsOutboundMission wcsOutboundMission = new WcsOutboundMission();
            wcsOutboundMission.setOutboundCode(outboundCode);
            wcsOutboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
            wcsOutboundMission.setContainerCode(containerCode);
            wcsOutboundMission.setSource(source);
            wcsOutboundMission.setDestination(destination);
            wcsOutboundMission.setCreateTime(LocalDateTime.now());
            wcsOutboundMission.setUpdateTime(LocalDateTime.now());
            wcsOutboundMission.setContainerType(TaskType.THREE_WORKBIN.getStatus());
            wcsOutboundMission.setPriority(priority);
            wcsOutboundMission.setTaskType(taskType);
            wcsOutboundMission.setIsNest(0);
            wcsOutboundMission.setWmsTaskId(wmsTaskId);
            wcsOutboundMission.setFloor(floor);
            wcsOutboundMission.setIsWms(1);
            boolean b2 = wcsOutboundMissionService.insertOutbound(wcsOutboundMission);
            if (!b2) {
                outbound.error("数据库异常，添加出库任务失败");
                throw new RuntimeException("数据库异常，添加出库任务失败");
            }
            //记录出库单明细
            WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails = new WcsThreeFloorOutboundDetails();
            wcsThreeFloorOutboundDetails.setWorkOrder(workOrder);
            wcsThreeFloorOutboundDetails.setContainerCode(containerCode);
            wcsThreeFloorOutboundDetails.setStatus(0);
            wcsThreeFloorOutboundDetails.setFloor(floor);
            wcsThreeFloorOutboundDetails.setRegion(region);
            wcsThreeFloorOutboundDetails.setAgvReleaseCompleted(0);
            boolean b1 = wcsThreeFloorOutboundDetailsService.insert(wcsThreeFloorOutboundDetails);
            if (!b1) {
                outbound.error("数据库异常，添加3楼出库单明细失败");
                throw new RuntimeException("数据库异常，添加3楼出库单明细失败");
            }
            //储位绑定栈板
//            CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
//            carrierUnbindDto.setSlotCategory(TaskType.STORAGE.getMessage());
//            carrierUnbindDto.setSlotCode(source);
//            carrierUnbindDto.setCarrierCategory("PALLET");
//            carrierUnbindDto.setCarrierType("1");
//            carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
//            agvSendService.carrierUnbind(carrierUnbindDto);

            //创建agv放货数据，等待agv申请放货请求
            WcsAgvReleaseGoods wcsAgvReleaseGoods = new WcsAgvReleaseGoods();
            wcsAgvReleaseGoods.setTaskCode(outboundCode);
            wcsAgvReleaseGoods.setCreateTime(LocalDateTime.now());
            wcsAgvReleaseGoods.setApplyReleaseGoodsStatus(TaskStatus.APPLY_RELEASE_GOODS_DEFAULT.getStatus());
            wcsAgvReleaseGoods.setPattern(TaskStatus.PATTERN_ANTI_CARGO.getStatus());
            wcsAgvReleaseGoods.setFloor(TaskType.THREE_FLOOR.getStatus());
            boolean b3 = wcsAgvReleaseGoodsService.insert(wcsAgvReleaseGoods);
            if (!b3) {
                outbound.error("创建agv放货表失败");
                throw new RuntimeException("创建agv放货表失败");
            }

            AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
            //todo 任务类型，根据现场情况来定
            agvTaskSubmitDto.setTaskType(TaskType.DX_3F_CTU_CK.getMessage());
            ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
            TargetRoute targetRoute = new TargetRoute();
            TargetRoute targetRoutes = new TargetRoute();
            //添加起点
            targetRoute.setSeq(0);
//            targetRoute.setType(TaskType.STORAGE.getMessage());
            targetRoute.setType(TaskType.STORAGE.getMessage());
            targetRoute.setCode(source);
            //添加终点
            targetRoutes.setSeq(1);
//            targetRoutes.setType(TaskType.SITE.getMessage());
            targetRoutes.setType(TaskType.STORAGE.getMessage());
            targetRoutes.setCode(destination);
            targetRouteList.add(targetRoute);
            targetRouteList.add(targetRoutes);
            agvTaskSubmitDto.setTargetRoute(targetRouteList);
            agvTaskSubmitDto.setRobotTaskCode(outboundCode);
            agvTaskSubmitDto.setInitPriority(priority);

            CarrierInfoDto carrierInfoDto = new CarrierInfoDto();
            carrierInfoDto.setCarrierCode(containerCode);
            carrierInfoDto.setCarrierType(TaskType.THREE_Task_assignment_Container_binding.getMessage());
            ArrayList<CarrierInfoDto> carrierInfoDtos = new ArrayList<>();
            carrierInfoDtos.add(carrierInfoDto);
            ExtraDto extraDto = new ExtraDto();
            extraDto.setCarrierInfo(carrierInfoDtos);
            agvTaskSubmitDto.setExtra(extraDto);
            String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
            if (taskCode == null) {
                outbound.error("海康AGV任务接收失败");
            }
        }
        return ReturnVo.OK();
    }

    @Transactional
    @Override
    public ReturnVo artificialInbound(DeliveryDto oneFloorOutboundDto) {
        outbound.info("人工入库请求参数：{}", oneFloorOutboundDto);
        String containerCode = oneFloorOutboundDto.getContainerCode();
        String source = oneFloorOutboundDto.getSource();
        String floor = oneFloorOutboundDto.getFloor();
        Integer deliveryType = oneFloorOutboundDto.getDeliveryType();
        Integer priority = oneFloorOutboundDto.getPriority();
        String wmsTaskId = oneFloorOutboundDto.getWmsTaskId();
        boolean nest = deliveryType !=1;
        //向wms发送储位申请入库
            InboundStorageApplyDto1 inboundStorageApplyDto = new InboundStorageApplyDto1();
            inboundStorageApplyDto.setContainerCode(containerCode);
            inboundStorageApplyDto.setFloor(floor);
            inboundStorageApplyDto.setNest(nest);
            List<NestingMiddleTable> nestingMiddleTables = nestingMiddleTableService.selectContainerCode(containerCode);
            if (!nestingMiddleTables.isEmpty()) {
                ArrayList<NestingMiddleTable> materialDaos = new ArrayList<>();
                for (NestingMiddleTable nestingMiddleTable : nestingMiddleTables) {
                    NestingMiddleTable nestingMiddleTable1 = new NestingMiddleTable();
                    BeanUtils.copyProperties(nestingMiddleTable,nestingMiddleTable1);
                    materialDaos.add(nestingMiddleTable1);
                }
                inboundStorageApplyDto.setData(materialDaos);
                String destination = wcsReportService.inboundStorageApply(inboundStorageApplyDto);
                if (destination != null) {
                    StringBuilder sb = new StringBuilder();
                    List<String> materialCodeList = nestingMiddleTables.stream().map(NestingMiddleTable::getMaterialCode).distinct().collect(Collectors.toList());
                    for (String materialCode : materialCodeList) {
                        if (sb.length() > 0) {
                            sb.append(",");
                        }
                        sb.append(materialCode);
                    }
                    String inboundCode = String.valueOf(idWorker.nextId());
                    List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectContainerCodeNotTask(containerCode);
                    if (!wcsInboundMissionList.isEmpty()){
                        return new ReturnVo(500,"载具已再AGV任务中",null);
                    }
                    //创建入库任务
                    String nestingJson = JsonUtil.toJson(materialDaos);
                    WcsInboundMission wcsInboundMission = new WcsInboundMission();
                    wcsInboundMission.setInboundCode(inboundCode);
                    wcsInboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                    wcsInboundMission.setContainerCode(containerCode);
                    wcsInboundMission.setSource(source);
                    wcsInboundMission.setDestination(destination);
                    if (TaskType.ONE_FLOOR.getMessage().equals(floor)) {
                        wcsInboundMission.setContainerType(TaskType.PALLET.getStatus());
                    } else if ((TaskType.TWO_FLOOR.getMessage().equals(floor))) {
                        wcsInboundMission.setContainerType(TaskType.WORKBIN.getStatus());
                    } else if ((TaskType.THREE_FLOOR.getMessage().equals(floor))) {
                        wcsInboundMission.setContainerType(TaskType.THREE_WORKBIN.getStatus());
                    }
                    wcsInboundMission.setCreateTime(LocalDateTime.now());
                    wcsInboundMission.setUpdateTime(LocalDateTime.now());
                    if (TaskType.inbound_ordinary.getStatus().equals(deliveryType)) {
                        wcsInboundMission.setInboundType(TaskType.inbound_manual.getStatus());
                    }else {
                        wcsInboundMission.setInboundType(deliveryType);
                    }
                    wcsInboundMission.setPriority(priority);
                    wcsInboundMission.setIsNest(0);
                    wcsInboundMission.setIsPlc(0);
                    wcsInboundMission.setIsWms(1);
                    wcsInboundMission.setFloor(floor);
                    wcsInboundMission.setWmsTaskId(wmsTaskId);
                    String materialCode = sb.toString();
                    wcsInboundMission.setMaterialCode(materialCode);
                    wcsInboundMission.setNestingJson(nestingJson);
                    boolean b1 = wcsInboundMissionService.insertInbound(wcsInboundMission);
                    if (!b1){
                        return ReturnVo.ng("入库任务创建失败");
                    }
                    //下发agv任务
                    AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                    //todo 任务类型，根据现场情况来定
                    agvTaskSubmitDto.setTaskType("PF-CTU-RUKU");
//                    agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
                    ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                    TargetRoute targetRoute = new TargetRoute();
                    TargetRoute targetRoutes = new TargetRoute();
                    //添加起点
                    targetRoute.setSeq(0);
                    targetRoute.setType(TaskType.STORAGE.getMessage());
                    targetRoute.setCode(source);
                    //添加终点
                    targetRoutes.setSeq(1);
                    targetRoutes.setType(TaskType.STORAGE.getMessage());
                    targetRoutes.setCode(destination);
                    targetRouteList.add(targetRoute);
                    targetRouteList.add(targetRoutes);
                    agvTaskSubmitDto.setTargetRoute(targetRouteList);
                    agvTaskSubmitDto.setRobotTaskCode(inboundCode);
                    agvTaskSubmitDto.setInitPriority(PriorityEnum.FOUR.getWeight());

                    CarrierInfoDto carrierInfoDto = new CarrierInfoDto();
                    carrierInfoDto.setCarrierCode(containerCode);
                    carrierInfoDto.setCarrierType(TaskType.THREE_Task_assignment_Container_binding.getMessage());
                    ArrayList<CarrierInfoDto> carrierInfoDtos = new ArrayList<>();
                    carrierInfoDtos.add(carrierInfoDto);
                    ExtraDto extraDto = new ExtraDto();
                    extraDto.setCarrierInfo(carrierInfoDtos);
                    agvTaskSubmitDto.setExtra(extraDto);
                    String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                    if (taskCode == null) {
                        outbound.error("下发agv任务失败");
                        throw new RuntimeException("下发agv任务失败");
                    }
                    return ReturnVo.OK();
                }
                return ReturnVo.ng("入库未找到合适储位");
            }
            return ReturnVo.ng("中间表未嵌套");
    }

    @Override
    public ReturnVo containerCodeTaskWhetherExist(TaskWhetherExistDto taskWhetherExistDto) {
        String containerCode = taskWhetherExistDto.getContainerCode();
        if (containerCode == null){
            return ReturnVo.ng("容器编码为空");
        }
        List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectContainerCodeNotTask(containerCode);
        List<WcsDeliveryMission> wcsDeliveryMissionList = wcsDeliveryMissionService.selectContainerCodeNoTask(containerCode);
        List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectContainerCodeNotTask(containerCode);
        if (!wcsInboundMissionList.isEmpty() || !wcsDeliveryMissionList.isEmpty() || !wcsOutboundMissionList.isEmpty()){
            return new ReturnVo(500,"载具已再AGV任务中",null);
        }
        return ReturnVo.OK();
    }

    @Override
    public ReturnVo storageRelease(StorageReleaseDto storageReleaseDto) {
        String storageCode = storageReleaseDto.getStorageCode();
        String containerCode = storageReleaseDto.getContainerCode();
        if (storageCode == null || containerCode == null){
            return ReturnVo.ng("AGV解绑请求参数不能为NULL");
        }
        CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
        carrierUnbindDto.setCarrierCategory("BOX");
        carrierUnbindDto.setCarrierCode(containerCode);
        carrierUnbindDto.setInvoke("UNBIND");
        carrierUnbindDto.setSlotCategory("BIN");
        carrierUnbindDto.setSlotCode(storageCode);
        AgvTaskSubmitVo agvTaskSubmitVo = agvSendService.carrierUnbind(carrierUnbindDto);
        if (agvTaskSubmitVo !=null){
            if ("SUCCESS".equals(agvTaskSubmitVo.getCode())){
                return ReturnVo.OK();
            }
            return ReturnVo.ng("AGV解绑失败,code不为SUCCESS");
        }
        return ReturnVo.ng("AGV解绑失败");
    }
}
